package mvpfoot;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;

import mvpfoot.listener.PermissionListener;

/**
 * Created by Chen qin lang on 2017/1/6.
 */
public class BaseActivity extends AppCompatActivity {

    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressDialog = new ProgressDialog(this);
    }

    protected void showDialog(String msg) {
        if (msg != null && progressDialog != null) {
            progressDialog.setMessage(msg);
            progressDialog.show();
        }
    }

    protected void hideDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }


    public void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
    private PermissionListener mListener;
    private final int SDK_PERMISSION_REQUEST = 127;

    public void requestRuntimePermission(String[] permissions, PermissionListener listener) {
        mListener=listener;
        ArrayList<String> permissionLists = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionLists.add(permission);
            }
        }
        if (!permissionLists.isEmpty()) {
           ActivityCompat.requestPermissions(this,permissionLists.toArray(new String[permissionLists.size()]),SDK_PERMISSION_REQUEST);
        } else {
            listener.granted();
        }
    }
    //请求的权限返回的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case SDK_PERMISSION_REQUEST:
                if (grantResults.length > 0) {
                    ArrayList<String> deniedLists = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        int grantResult = grantResults[i];
                        String permission = permissions[i];
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
                            deniedLists.add(permission);
                        }
                    }
                    if (deniedLists.isEmpty()) {
                        mListener.granted();
                    } else {
                        mListener.denied(deniedLists);
                    }
                    break;
                }
        }
    }
}
